<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="ActivityName" label="活动名称">
        <uni-data-picker v-model="formData.ActivityName" collection="abu_activity" field="ActivityName as text,_id as value" trim="both"></uni-data-picker>
      </uni-forms-item>
      <uni-forms-item name="community_id" label="社区名称">
        <uni-data-picker v-model="formData.community_id" collection="abu_community" field="name as text,_id as value" trim="both"></uni-data-picker>
      </uni-forms-item>
      <uni-forms-item name="user_id" label="用户昵称">
        <uni-data-picker v-model="formData.user_id" collection="uni-id-users" field="username as text, _id as value"></uni-data-picker>
      </uni-forms-item>
      <uni-forms-item name="Name" label="真实姓名" required>
        <uni-easyinput placeholder="参与者的真实姓名" v-model="formData.Name" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="Contact" label="联系方式" required>
        <uni-easyinput placeholder="联系方式，可以是电话号码或电子邮箱" v-model="formData.Contact" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="RegistrationStatus" label="报名状态">
        <uni-data-checkbox v-model="formData.RegistrationStatus" :localdata="formOptions.RegistrationStatus_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="Notes" label="备注">
        <uni-easyinput placeholder="关于参与者或报名的额外信息或特殊需求" v-model="formData.Notes" trim="both"></uni-easyinput>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/abu_baoming.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'abu_baoming';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "ActivityName": "",
        "community_id": "",
        "user_id": "",
        "Name": "",
        "Contact": "",
        "RegistrationStatus": null,
        "Notes": ""
      }
      return {
        formData,
        formOptions: {
          "RegistrationStatus_localdata": [
            {
              "text": "已报名",
              "value": 0
            },
            {
              "text": "待审核",
              "value": 1
            },
            {
              "text": "已取消",
              "value": 2
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("ActivityName,community_id,user_id,Name,Contact,RegistrationStatus,Notes").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
            
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>
